/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package General;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author NganNLK60334
 */
public class XMLUtils {

    public static void marshallClass(Object obj, String filePath) {
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller mar = context.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            File file = new File(filePath);
            mar.marshal(obj, file);
        } catch (JAXBException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    public static Node parseDOM (String xmlFile){
        try{
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File(xmlFile));
            return doc;

        } catch (Exception e){
             Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, e);
        }
        return null;
    }

    public static Object unmarshallClass(String xsdPath, String xmlPath, Class boundClass) throws SAXException {
        try {
            JAXBContext context = JAXBContext.newInstance(boundClass);
            Unmarshaller mar = context.createUnmarshaller();
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema sm = sf.newSchema(new File(xsdPath));
            mar.setSchema(sm);

            File file = new File(xmlPath);
            Object obj = mar.unmarshal(file);
            return obj;
        } catch (JAXBException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);

        }
        return null;
    }

    public static void writeXMLtoFile(Node node, String filePath) {
        try {
            TransformerFactory tff = TransformerFactory.newInstance();
            Transformer trans = tff.newTransformer();
            Source src = new DOMSource(node);
            File file = new File(filePath);
            Result result = new StreamResult(file);
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            trans.transform(src, result);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    public static Document buildDOMTree(String filePath) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            //dbf.setNamespaceAware(false);
            DocumentBuilder db = dbf.newDocumentBuilder();
            File file = new File(filePath);
            Document doc = db.parse(file);
            return doc;
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static String covertToString(String xslPath, String xmlPath, Map<String, Object> param) {
        String result = "";
        try {
            StreamSource xslStream = new StreamSource(xslPath);
            StreamSource xmlStream = new StreamSource(xmlPath);
            TransformerFactory tff = TransformerFactory.newInstance();
            Transformer tf = tff.newTransformer(xslStream);
            StringWriter sw = new StringWriter();
            StreamResult rs = new StreamResult(sw);
            Set<String> keys = param.keySet();
            for (String key : keys){
                tf.setParameter(key, param.get(key));
            }
            tf.transform(xmlStream, rs);
            result = sw.toString();
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex){
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
}
